﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class CmsServer
    {
        public void Add(KeRui.Model.CmsServer model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerName", SqlDbType.NVarChar, 50), new SqlParameter("@ServerUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@ServerKey", SqlDbType.VarChar, 50), new SqlParameter("@ServerDir", SqlDbType.VarChar, 200), new SqlParameter("@IsUsing", SqlDbType.Int, 4), new SqlParameter("@LicenseID", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ServerName;
            parameters[1].Value = model.ServerUrl;
            parameters[2].Value = model.ServerKey;
            parameters[3].Value = model.ServerDir;
            parameters[4].Value = model.IsUsing;
            parameters[5].Value = model.LicenseID;
            parameters[6].Value = model.OrderID;
            DbHelperSQL.RunProcedure("CmsServer_ADD", parameters, out num);
        }

        public int Delete(int ServerID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerID", SqlDbType.Int, 4) };
            parameters[0].Value = ServerID;
            return DbHelperSQL.RunProcedure("CmsServer_Delete", parameters, out num);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ServerID,ServerName,ServerIP,ServerUrl,ServerKey,IsUsing,LicenseID,Domain,SiteNum,OrderID,CreateTime ");
            builder.Append(" FROM CmsServer ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ServerID,ServerName,ServerIP,ServerUrl,ServerKey,IsUsing,LicenseID,Domain,SiteNum,OrderID,CreateTime ");
            builder.Append(" FROM CmsServer ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public KeRui.Model.CmsServer GetModel(int ServerID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerID", SqlDbType.Int, 4) };
            parameters[0].Value = ServerID;
            KeRui.Model.CmsServer server = new KeRui.Model.CmsServer();
            DataSet set = DbHelperSQL.RunProcedure("CmsServer_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    server.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                server.ServerName = set.Tables[0].Rows[0]["ServerName"].ToString();
                server.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                server.ServerUrl = set.Tables[0].Rows[0]["ServerUrl"].ToString();
                server.ServerKey = set.Tables[0].Rows[0]["ServerKey"].ToString();
                server.ServerDir = set.Tables[0].Rows[0]["ServerDir"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    server.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["LicenseID"].ToString() != "")
                {
                    server.LicenseID = int.Parse(set.Tables[0].Rows[0]["LicenseID"].ToString());
                }
                server.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                if (set.Tables[0].Rows[0]["SiteNum"].ToString() != "")
                {
                    server.SiteNum = int.Parse(set.Tables[0].Rows[0]["SiteNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    server.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    server.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return server;
            }
            return null;
        }

        public KeRui.Model.CmsServer GetModel(string ServerIP)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerIP", SqlDbType.VarChar, 15) };
            parameters[0].Value = ServerIP;
            KeRui.Model.CmsServer server = new KeRui.Model.CmsServer();
            DataSet set = DbHelperSQL.RunProcedure("CmsServer_GetModelbyIP", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    server.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                server.ServerName = set.Tables[0].Rows[0]["ServerName"].ToString();
                server.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                server.ServerUrl = set.Tables[0].Rows[0]["ServerUrl"].ToString();
                server.ServerKey = set.Tables[0].Rows[0]["ServerKey"].ToString();
                server.ServerDir = set.Tables[0].Rows[0]["ServerDir"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    server.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["LicenseID"].ToString() != "")
                {
                    server.LicenseID = int.Parse(set.Tables[0].Rows[0]["LicenseID"].ToString());
                }
                server.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                if (set.Tables[0].Rows[0]["SiteNum"].ToString() != "")
                {
                    server.SiteNum = int.Parse(set.Tables[0].Rows[0]["SiteNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    server.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    server.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return server;
            }
            return null;
        }

        public int GetServerIDByIP(string IP)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerIP", SqlDbType.VarChar, 0xff) };
            parameters[0].Value = IP;
            return DbHelperSQL.RunProcedure("CmsServer_GetServerIDByIP", parameters, out num);
        }

        public int GetServerIDByUrl(string ServerUrl)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerUrl", SqlDbType.VarChar, 0xff) };
            parameters[0].Value = ServerUrl;
            return DbHelperSQL.RunProcedure("CmsServer_GetServerIDByUrl", parameters, out num);
        }

        public void Update(KeRui.Model.CmsServer model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerID", SqlDbType.Int, 4), new SqlParameter("@ServerName", SqlDbType.NVarChar, 50), new SqlParameter("@ServerUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@ServerKey", SqlDbType.VarChar, 50), new SqlParameter("@ServerDir", SqlDbType.VarChar, 200), new SqlParameter("@IsUsing", SqlDbType.Int, 4), new SqlParameter("@LicenseID", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ServerID;
            parameters[1].Value = model.ServerName;
            parameters[2].Value = model.ServerUrl;
            parameters[3].Value = model.ServerKey;
            parameters[4].Value = model.ServerDir;
            parameters[5].Value = model.IsUsing;
            parameters[6].Value = model.LicenseID;
            parameters[7].Value = model.OrderID;
            DbHelperSQL.RunProcedure("CmsServer_Update", parameters, out num);
        }
    }
}

